package isssr.bean.boundary;

import isssr.bean.entity.User;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

public class UserObjectConverter implements Serializable, Converter {

	
	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		
		//System.out.println("entry in getAsObject");
		Map<String, Object> view_map = context.getCurrentInstance().getViewRoot().getViewMap();
		ActivityGOBoundary boundary = (ActivityGOBoundary) view_map.get("activityGOBoundary");
		List<User> source  = boundary.getElegible_super_list_source();
		List<User> target  = boundary.getElegible_super_list_target();
		
		//System.out.println("size della lista: " + super_list.size());
		if(value != null)
		{
			for(int i=0; i<source.size(); i++)
			{
				User superUser 		= source.get(i);
				Integer superUserId = superUser.getId();
				
				if(superUserId.toString().equals(value))
				{
					//System.out.println(super_list.get(i).getUsername());
					return source.get(i);
				}
			}
			
			for(int i=0; i<target.size(); i++)
			{
				User superUser 		= target.get(i);
				Integer superUserId = superUser.getId();
				
				if(superUserId.toString().equals(value))
				{
					//System.out.println(super_list.get(i).getUsername());
					return target.get(i);
				}
			}
		
		}

		return null;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		
		//System.out.println("entry in getAsString");
		//System.out.println(arg2.toString());
		
		String id = null;
		if(arg2 != null)
		id = String.valueOf(arg2);
		//System.out.println("----------------------------------------------------------------------");
		/*User goalEnactor = (User) arg2;
		String username = goalEnactor.getUsername();
		
		*/
		
		
		/*
		String id = null;
		if(arg2 != null)
			id = String.valueOf(((User) arg2).getId());
		*/
		
		
		return id;
	}

}
